package com.zhixinglu.www.spring.boot.dsl.json.controller;

import com.dslplatform.json.DslJson;
import com.dslplatform.json.JsonReader;
import com.dslplatform.json.JsonWriter;
import com.dslplatform.json.PrettifyOutputStream;
import com.dslplatform.json.runtime.Settings;
import com.zhixinglu.www.spring.boot.dsl.json.dto.SavePointDTO;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

/**
 * <p>
 * DslJsonController
 * </p>
 *
 * @author weixin
 * @date 2023/9/23 16:47
 */
public class DslJsonController {

    public static void main(String[] args) throws IOException {
        SavePointDTO savePointDTO = new SavePointDTO();
        savePointDTO.setPoiId(1L);
        savePointDTO.setPoiName("光明顶");
        savePointDTO.setPoiContent("光明顶是安徽黄山第二高峰，海拔1860米，是黄山前山和后山的分界线。2、峰顶平坦高旷，面积约6万平方米，视野开阔，日光充足，故而得名“光明顶”。3、光明顶还是观赏日出的胜地，号称从此处可以看见全部黄山36峰景色。4、明代普门法师曾在此处建大悲院，著名作家袁中道等人均在此留下游记。5、由于地势平坦，所以是黄山看日出、观云海的最佳地点之一。6、在天气条件极佳的情况下，可见波江座主星水委一。");
        savePointDTO.setPoiType(1);
        savePointDTO.setBuildingHeight(19.0);
        savePointDTO.setRegionalCoordinate("39.9973,116.45923;39.997435,116.459252;39.997448,116.458523;39.997721,116.458136;39.997622,116.458006;39.997308,116.458457");
        savePointDTO.setLatitude(39.997376);
        savePointDTO.setLongitude(116.458678);
        savePointDTO.setElevation(30.0);
        savePointDTO.setHeightOffset(0);
        savePointDTO.setScenicId(1L);

        DslJson<Object> dslJson = new DslJson<>(Settings.withRuntime().allowArrayFormat(true).includeServiceLoader());
        JsonWriter writer = dslJson.newWriter();
        dslJson.serialize(writer, savePointDTO);

        // 获取当前时间
        long startTime = System.currentTimeMillis();

        //ByteArrayOutputStream os = new ByteArrayOutputStream();
        //dslJson.serialize(savePointDTO, new PrettifyOutputStream(os));
        //System.out.println(os);

        // 238 毫秒
        for (int i = 0; i < 100000; i++) {
            // JSON字符串转SavePointDTO对象
            byte[] buffer = writer.getByteBuffer();
            int size = writer.size();
            dslJson.deserialize(SavePointDTO.class, buffer, size);
        }

        // 获取操作结束的时间
        long endTime = System.currentTimeMillis();
        // 计算时长
        long duration = endTime - startTime;
        // 输出时长
        System.out.println("操作耗时：" + duration + " 毫秒");
    }

}
